<script>History.pushState(null, "Epicrisis", "<?php echo $this->here; ?>");</script>
<script type="text/javascript">
		// Activo el Mejorador de SELECT
		jQuery(".chzn-select").chosen(); 
		jQuery(".chzn-select-deselect").chosen({allow_single_deselect:true});
		jQuery(".chzn-select").chosen({no_results_text: "No hay resultados"});

		jQuery(document).ready(function(){ 
			jQuery( "#tabs" ).tabs();
		});
		
		// Activo el Texto Enriquecido
		jQuery('textarea.tinymce').tinymce({
			// Location of TinyMCE script
			script_url : '/js/tiny_mce/tiny_mce.js',

            // General options
            theme : "simple",

            // Theme options
            theme_advanced_buttons1 : "|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,bullist,numlist",
            theme_advanced_toolbar_location : "top",
            theme_advanced_toolbar_align : "left",
            theme_advanced_resizing : false, 

		});

</script>
<?php
	if(!empty($this->data['User']['foto_firma'])){
		$imagen_paciente = $this->webroot.'uploads/'.$this->data['User']['foto_firma'];
	}
?>
<div class="clientsUsers form">
<?php echo $this->Form->create('ClientsUser',array('enctype'=>"multipart/form-data"));?>

<div id="tabs">
	<ul>
		<li><a href="#tabs-1"><?php __("Datos del Usuario")?></a></li>
		<?php if($asocia_financiador_profesional==1){?>
			<li><a href="#tabs-2"><?php __("Financiador")?></a></li>
		<?php } ?>

		<?php if ($asocia_productos_profesional==1 ) {?>
			<li><a href="#tabs-3"><?php __("Productos")?></a></li>
		<?php } ?>

		<?php if($carga_matriz_productos_financiadores==1){?>
			<li><a href="#tabs-4"><?php __("Productos / Financiadores")?></a></li>
		<?php } ?>
		<li><a href="#tabs-5"><?php __("Recetario")?></a></li>
	</ul>
	<div id="tabs-1">
	
	<?php
//		echo $this->Form->input('id');
		echo $this->Form->input('User.id',array('type' => 'hidden', 'value' => $id_user));
//		echo $this->Form->input('client_id', array('type' => 'hidden', 'value' => $id_cliente));


 

		echo $this->Form->input('User.tratamiento',array( 'label' => __('Prefijo',true)));
		echo $this->Form->input('User.lastname',array( 'label' => __('Apellido',true)));
		echo $this->Form->input('User.name',array( 'label' => __('Nombre',true)));
	
		echo $this->Form->input('User.email',array( 'label' => __('E-mail',true)));

		


		echo "<table style='width:100%'><tr><td style='width:30%'>";
		echo $this->Form->input('User.matricula',array( 'label' => __('Matrícula',true), 'style' => 'width:150px;'));
		echo "</td><td>";
		echo $this->Form->input('User.foto_firma',array( 'type' => 'file','label' => __('Firma Digitalizada',true), 'style' => 'width:85%;'));


		if(!empty($this->data['User']['foto_firma'])){
			echo '
					<table style="width:100%;">
						<tr>	
							<td width="50"><img src="/uploads/'.$this->data['User']['foto_firma'].'" style="height:40px;margin:5px;"></td>
							<td style="vertical-align: middle;"><input type="checkbox" name="eliminar_foto" style="width: 15px;">'.__('Eliminar firma',true).'</td>
						</tr>
					</table>

			';

		}
		echo "</td></tr></table>";


		echo $this->Form->input('SpecialtiesUser.specialty_id', array(
										'class' 			=> 'chzn-select', 
										'data-placeholder' 	=> __('Seleccione las especialidades',true),
										'options' 			=> $specialties, 
										'selected' 			=> $especialidades_usuario, 
										'multiple' 			=> 'true',
										'label' 			=> 'Especialidades', 
										'style' 			=> 'width:350px;'
									)
								);

		echo $this->Form->input('User.telefono',array( 'label' => __('Teléfono',true)));
		echo $this->Form->input('User.fecha_nacimiento',array( 
										'label' 	 => __('Fecha de Nacimiento',true), 
										'empty' 	 => '---', 
										'dateFormat' => 'DMY', 
										'minYear' 	 => date('Y') - 100, 
										'maxYear' 	 => date('Y') 
									)
								);

		echo $this->Form->input('User.typodocument_id',array( 
										'label' => __('Tipo de Documento',true),
										'empty' => '--- Seleccione uno ---'
									)
								);

		echo $this->Form->input('User.nro_documento',array( 'label' => __('Nro. de Documento',true)));
		echo "<table style='width:100%'><tr><td style='width:50%'>";
		echo $this->Form->input('User.country_id', array('label' => __('País',true)));
		echo "</td><td>";
		echo $this->Form->input('User.idioma', array(
												'label'     => 'Idioma',
												'options' 	=> $Idiomas, 
												'selected' 	=> $this->data['User']['idioma']
												));
		echo "</td></tr></table>";

		echo $this->Form->input('User.username',array( 'label' => __('Usuario',true)));
		echo $this->Form->input('User.nueva_contrasena',array( 
										'label'  => 'Nueva Contraseña',
										'type'	 =>'password',
										'before' => '<a href="#" onclick="jQuery(\'#cambiarcontrasena\').toggle();">Cambiar Contraseña</a><div id="cambiarcontrasena" style="display:none">',
										'after'  => '</div>'
									)
								);

		echo $this->Form->input('Roles.rol_id', array(
										'class' 			=> 'chzn-select', 
										'data-placeholder'  => __('Seleccione los roles',true),
										'options' 			=> $roles, 
										'selected' 			=> $roles_usuario, 
										'multiple' 			=> 'true',
										'label' 			=> 'Roles', 
										'empty'				=> __('Seleccione los roles',true), 
										'style' 			=> 'width:600px;'
									)
								);		
								
		echo $this->Form->input('Sucursales.sucursal_id', array(
										'class' 			=> 'chzn-select', 
										'data-placeholder'  => __('Seleccione las Sucursales',true),
										'options' 			=> $Sucursales, 
										'selected' 			=> $sucursales_usuario, 
										'multiple' 			=> 'true',
										'label' 			=> 'Sucursales', 
										'empty'				=> __('Seleccione las Sucursales',true), 
										'style' 			=> 'width:600px;'
									)
								);		
	?>

	</div>

	<?php if($asocia_financiador_profesional==1){?>
	<div id="tabs-2">
		<?php //pr($insurances_user);?>
		<table width='100%'>
			<tr>
				<td width='40%'>
<?php
		echo $this->Form->input('Insurances.insurance_id', array(
										'class' 			=> 'chzn-select', 
										'id'				=> 'os_select',
										'data-placeholder'  => __('Seleccione las Obras Sociales',true),
										'options' 			=> $insurances, 
										'selected' 			=> $insurances_user, 
										'multiple' 			=> 'false',
										'label' 			=> 'Obras Sociales', 
										'empty'				=> __('Seleccione las Obras Sociales',true), 
										'style' 			=> 'width:300px;'
									)
								);		
?>
				</td>
				<td style='vertical-align:bottom;' with='40%'>
					<?php $contador = count($insurances_user);?>
					<?php echo $this->Form->input('Insurances.contador',array( 'type' => 'hidden', 'id'=>'contador', 'value' => $contador)); ?>
					<?php echo $this->Form->input('Insurances.plan',array( 'label' => __('Plan',true), 'id'=>'text_plan')); ?>
				</td>
				<td style='vertical-align:bottom;'><input type="button" value="Agregar" id="agregar_os" class="btn" style="cursor:pointer;"></td>				
			</tr>
		</table>

		<table id="lista_os">
			<?php if( $contador>0 ) { $i=0;?>
				<?php foreach($insurances_user as $iu){ ?>
					<?php if ($iu['InsurancesUser']['insurance_id']!=478) { ?>
					<tr>
						<td class='problema_texto' style='padding:5px;cursor:pointer;'><img src='/images/delete.gif' class='sv_delete' title='Eliminar Registro' /></td>
						<td class='problema_texto' style='width:280px;overflow:hidden;padding:5px;'><?php echo $iu['Insurance']['name'];?>
							<input name='data[Planes][<?php echo $i;?>][insurancesuser_id]'  value='<?php echo $iu['InsurancesUser']['id'];?>' type='hidden'/>
							<input name='data[Planes][<?php echo $i;?>][os_id]'              value='<?php echo $iu['InsurancesUser']['insurance_id'];?>' type='hidden'/>
							<input name='data[Planes][<?php echo $i;?>][os_texto]'           value='<?php echo $iu['Insurance']['name'];?>'              type='hidden'/>				
							<input name='data[Planes][<?php echo $i;?>][os_plan]'            value='<?php echo $iu['InsurancesUser']['plan'];?>'         type='hidden'/>
						</td>
						<td class='problema_texto'  style='padding:5px;'><?php echo $iu['InsurancesUser']['plan'];?></td>
					</tr>
					<?php } else { ?>
							<input name='data[Planes][<?php echo $i;?>][insurancesuser_id]'  value='<?php echo $iu['InsurancesUser']['id'];?>' type='hidden'/>
							<input name='data[Planes][<?php echo $i;?>][os_id]'              value='<?php echo $iu['InsurancesUser']['insurance_id'];?>' type='hidden'/>
							<input name='data[Planes][<?php echo $i;?>][os_texto]'           value='<?php echo $iu['Insurance']['name'];?>'              type='hidden'/>				
							<input name='data[Planes][<?php echo $i;?>][os_plan]'            value='<?php echo $iu['InsurancesUser']['plan'];?>'         type='hidden'/>

					<?php } ?>
					<?php $i++; ?>
				<?php } ?>
			<?php } ?>
		</table>							

	</div>
	<?php } //if($asocia_financiador_profesional==1)?>



	<?php if ($asocia_productos_profesional==1 ) {?>
	<div id="tabs-3">
<?php
		echo $this->Form->input('ProductsUser.product_id', array(
										'class' 			=> 'chzn-select', 
										'data-placeholder' 	=> __('Seleccione los productos',true),
										'options' 			=> $products, 
										'selected' 			=> $products_user, 
										'multiple' 			=> 'true',
										'label' 			=> 'Productos', 
										'style' 			=> 'width:730px;'
									)
								);

?>
	</div>
	<?php } //endif asocia_productos_profesional ?>


	<?php if($carga_matriz_productos_financiadores==1){?>
	<div id="tabs-4">
		<!-- ----------------------------- Productos / Financiadores -->

		Moneda: 
		<select name='data[moneda]'>
			<?php foreach($Monedas as $moneda){ ?>
				<?php if ($moneda['Money']['id']==$moneda_elegida) { $sel='selected=selected'; } else { $sel=''; } ?>
				<option value='<?php echo $moneda['Money']['id'];?>' <?php echo $sel;?> >
					<?php echo $moneda['Money']['name'];?> [<?php echo $moneda['Money']['simbolo'];?>]
				</option>
			<?php } // end foreach $Monedas ?>
		</select>

		<table width='100%' id='tabla_gral'>
			<tr>
				<th style='text-align:left; padding-left:10px;'><?php __("Financiador / Plan")?></th>
				<?php foreach($productos as $p) { ?>
					<th style='text-align:right; padding-right:10px;'><?php echo $p['Product']['name'];?></th>
				<?php } ?>
			</tr>
			<?php foreach($grilla as $g) { ?>
				<tr>
					<td>
<?php 
	if($g['insurance_id'] == 478){ // Si es PARTICULAR
		echo $g['name'];
	}else{
		echo $g['name'] ." - ". $g['plan'];
	}
?>
					</td>
					<?php foreach($g['productos'] as $p) { ?>
						<td style='text-align:right;'>
							<input type='hidden' name='data[grilla][<?php echo $g['insurance_user_id'];?>][<?php echo $p['product_id'];?>][insurance_user_id]' 
												 value='<?php echo $g['insurance_user_id'];?>' >

							<input type='hidden' name='data[grilla][<?php echo $g['insurance_user_id'];?>][<?php echo $p['product_id'];?>][product_id]' 
												 value='<?php echo $p['product_id'];?>'	>
							
							<input type='text'	name='data[grilla][<?php echo $g['insurance_user_id'];?>][<?php echo $p['product_id'];?>][importe]' 
												value='<?php echo number_format($p['importe'],2);?>'	
												style='text-align:right; width:60px;'>
					<?php } ?>

				</td>
			<?php } ?>
				
		</table>

	</div>
	<?php } //if($carga_matriz_productos_financiadores==1)?>	



	<div id="tabs-5">
		<input type='hidden' name='data[Recetario][id]' value='<?php echo $Recetario['Recetario']['id'];?>' />
		Encabezado del Recetario:<br>
		<textarea name='data[Recetario][encabezado]' style="width: 97%;height: 200px;" id="RecetarioEncabezado" class="tinymce"><?php echo $Recetario['Recetario']['encabezado'];?></textarea>
		<br>Pie de Página:<br>
		<textarea name='data[Recetario][pie]' style="width: 97%;height: 200px;" id="RecetarioPie" class="tinymce"><?php echo $Recetario['Recetario']['pie'];?></textarea>		
	</div>
	
</div>
	<?php echo $this->Form->end(__('Modificar', true));?>
</div>

<?php //pr($insurances_user);?>

<script>
	jQuery(function() {
		
	      jQuery("#text_plan").autocomplete("/ajax/obtiene_planes_os/", {
	        width: 260,
	        selectFirst: true
	      });

		
		//  Agregar opcion
		jQuery("#agregar_os").click(function(evento){
				
			var os_id             = jQuery("#os_select").val();
			var os_texto          = jQuery('#os_select option:selected').text();
			var plan              = jQuery("#text_plan").val();
			var contador          = jQuery("#contador").val() * 1;
			contador = contador + 1;
			jQuery("#contador").val(contador);

			if (os_id > 0 && plan != '') {
				var tds = "<tr>";
				tds += "<td class='problema_texto' style='padding:5px;cursor:pointer;'><img src='/images/delete.gif' class='sv_delete' title='Eliminar Registro' /></td>";
				tds += "<td class='problema_texto' style='width:280px;overflow:hidden;padding:5px;'>" + os_texto;
				tds += "<input name='data[Planes]["+ contador +"][insurancesuser_id]' value='n_"+ contador +"'type='hidden'/>";			
				tds += "<input name='data[Planes]["+ contador +"][os_id]' value='"+ os_id +"'type='hidden'/>";			
				tds += "<input name='data[Planes]["+ contador +"][os_texto]' value='"+ os_texto +"'type='hidden'/>";						
				tds += "<input name='data[Planes]["+ contador +"][os_plan]' value='"+ plan +"'type='hidden'/>";										
				tds += "</td>";
				tds += "<td class='problema_texto'  style='padding:5px;'>"+ plan + "</td>";
			
				tds += "</tr>";
				jQuery("#lista_os").append(tds);
			}			

			jQuery('#text_plan').val('');
		});
		   
		// event listener for delete button 
		jQuery(".sv_delete").live('click', function(event) {
			jQuery(this).parent().parent().remove();
		});    
	})
</script>